<?php

namespace app\admin\controller;
use app\BaseController;
use think\facade\Db;

class Category extends BaseController {

  public function info(){
    $id = request()->param('id');
    if (!$id) {
      returnJson(-2, '参数错误');
    }
    $goods = Db::table('shop_category')->where('id', $id)->find();
    if (!$goods) {
      returnJson(-2, '查询失败！');
    }
    returnJson(0, '成功！', $goods);
  }

  public function list () {
    $request = request()->param();

    $categoryList = Db::table('shop_category')->order('id', 'asc')->select();
    if (!$categoryList) {
      returnJson(-2, '查询失败！');
    }
    returnJson(0, '成功！', ["rows"=>$categoryList]);
  }

  public function add() {
    $request = request()->param();

    $data = [
      'category_name' => $request['category_name'],
    ];

    $save = Db::table('shop_category')->save($data);
    if ($save) {
      returnJson(0, '添加成功！');
    }
    returnJson(-2, '添加失败！');
  
  }

  public function update (){
    $request = request()->param();
    if (!isset($request['category_name']) || !isset($request['id'])) {
      returnJson(-2, '参数错误');
    }

    $id = $request['id'];
    $data = [
      'category_name' => $request['category_name'],
    ];
    
    $update = Db::table('shop_category')->where('id', $id)->update($data);
    if ($update >= 0) {
      returnJson(0, '修改成功！');
    }
    returnJson(-2, '修改失败！');
  }

  public function delete () {
    $ids = request()->param('ids');
    $idsArr = explode(',', $ids);
    
    if (is_array($idsArr)){
      $del = Db::table('shop_category')->delete($idsArr);
      returnJson(0, '删除成功！');
    }
    returnJson(-2, '删除失败！');
  }
  
}